package com.dao;

import javax.naming.NamingException;
import java.sql.*;

/**
 * @author :wn
 * @program : 项目五登录界面
 * @descript :
 * @create :2020-11-06 22:33
 */

public class StudentDao implements DaoImpl {


    @Override
    public boolean queryMethod() {
       return false;
    }
    public ResultSet quaryMethod() throws SQLException, NamingException {
            ResultSet rs=null;
            Connection    con = getConnection("java:comp/env/jdbc/studentInformation");
            String sql="select * from student";
            Statement st=con.createStatement();
            rs=st.executeQuery(sql);
            return rs;
    }


    @Override
    public void updateWay() {


    }
    public boolean updateWay(String name,String age,String sex,String sdept) throws SQLException, NamingException {
        boolean updateWether=false;
        Connection   con = getConnection("java:comp/env/jdbc/studentInformation");
        String sql="update student set age='"+age+"',sex='"+sex+"',sdept='"+sdept+"' where  name='"+name+"' ";
        Statement st=con.createStatement();
        if(st.executeUpdate(sql)>0){
            updateWether=true;
        }

        con.close();
        return updateWether;
    }

    @Override
    public void addWay() {

    }
    public boolean addWay(String name,String age,String sex,String sdept) throws SQLException, NamingException {
        boolean whetherToAdd=false;
             Connection   con = getConnection("java:comp/env/jdbc/studentInformation");
             String sql="insert into student(name,age,sex,sdept) values(?,?,?,?)";
             PreparedStatement pst=con.prepareStatement(sql);
             pst.setString(1,name);
             pst.setString(2,age);
             pst.setString(3,sex);
             pst.setString(4,sdept);
            int i= pst.executeUpdate();
            if(i>0){
                whetherToAdd=true;
            }
             con.close();
            return whetherToAdd;
    }

    @Override
    public void deletWay() {

    }
    public boolean deletMethod(String name) throws SQLException, NamingException {
        boolean deleteWether=false;
        Connection   con = getConnection("java:comp/env/jdbc/studentInformation");
        String sql="delete from student where name='"+name+"'";
        Statement st=con.createStatement();
        if(st.executeUpdate(sql)>0){
            deleteWether =true;
        }
        return deleteWether;
    }
}
